package com.chengyu.core.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chengyu.core.component.OperationLog;
import com.chengyu.core.dao.ReceiptUserDao;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.exception.CommonException;
import com.chengyu.core.exception.ServiceException;

import com.chengyu.core.model.ReceiptUser;
import com.chengyu.core.model.UmsMember;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.request.PageParamRequest;
import com.chengyu.core.service.ReceiptUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单发票信息 前端控制器
 * </p>
 *
 * @author macro
 * @since 2024-01-17
 */
@RestController
@RequestMapping("/user/receipt")
public class ReceiptUserController  extends UserBaseController {
    @Autowired
    private ReceiptUserService receiptUserService;


    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "cid", value = "分类id", dataType = "int"),
            @ApiImplicitParam(name = "type", value = "类型", dataType = "int"),
            @ApiImplicitParam(name = "title", value = "标题", dataType = "string"),
//            @ApiImplicitParam(name = "author", value = "作者", dataType = "string")
    })
    public CommonResult<CommonPage<ReceiptUser>> getList(@Validated ReceiptUser request, @Validated PageParamRequest pageParamRequest) throws ServiceException {

        Integer uid = getCurrentMemberId();
        request.setUid(uid);

        return com.chengyu.core.entity.CommonResult.success(CommonPage.restPage(receiptUserService.getList(request, pageParamRequest)));
    }


    @ApiOperation(value = "详情")
    @ResponseBody
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public com.chengyu.core.entity.CommonResult<ReceiptUser> detail(@PathVariable(value = "id") Integer id) {
        return com.chengyu.core.entity.CommonResult.success(receiptUserService.getVoByFront(id));
    }



    @ApiOperation(value = "添加")
    @OperationLog
    @ResponseBody
    @RequestMapping(value = {"/add"}, method = RequestMethod.POST)
    public com.chengyu.core.entity.CommonResult<String> editSubmit(@RequestBody @Validated  ReceiptUser receiptUser) throws CommonException, ServiceException {

        System.out.println("submitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmitsubmit");
        System.out.println(receiptUser);

        Integer uid = getCurrentMemberId();
        receiptUser.setUid(uid);


        if(receiptUser.getIsDefault().equals(1)){
            UpdateWrapper<ReceiptUser> wrapper = Wrappers.update();
            wrapper.setSql("is_default = 0");
            wrapper.eq("uid", uid);
            receiptUserService.update(wrapper);
        }

        receiptUserService.pushReceiptUser(receiptUser);


//		goodsService.publishGoodsNew(getCurrentShop(), publishForm);
        return com.chengyu.core.entity.CommonResult.success(null);
    }
    @ApiOperation(value = "编辑")
    @OperationLog
    @ResponseBody
    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.POST)
    public com.chengyu.core.entity.CommonResult<String> update(@PathVariable Integer id,@RequestBody  ReceiptUser receiptUser) throws CommonException, ServiceException {
        System.out.println("updateupdateupdateupdateupdateupdateupdateupdateupdate");

        UmsMember member = getCurrentMember();
        if(!id.equals(0) && id>0){
            receiptUser.setId(id);
        }
        Integer uid  = getCurrentMemberId();

        System.out.println(id);


        System.out.println(uid);
        System.out.println(receiptUser.getUid());

        ReceiptUser  old_receiptUser  = receiptUserService.getById(id);
        if(old_receiptUser==null  || uid !=old_receiptUser.getUid()){
                return com.chengyu.core.entity.CommonResult.failed();
        }
        receiptUser.setUid(uid);



        if(receiptUser.getIsDefault().equals(1)){
            UpdateWrapper<ReceiptUser> wrapper = Wrappers.update();
            wrapper.setSql("is_default = 0");
            wrapper.eq("uid", uid);
            receiptUserService.update(wrapper);
        }


        receiptUserService.pushReceiptUser(receiptUser);
//		goodsService.publishGoodsNew(getCurrentShop(), publishForm);
        return com.chengyu.core.entity.CommonResult.success(null);
    }


    @ApiOperation(value = "删除")
    @OperationLog
    @ResponseBody
    @RequestMapping(value = {"/del/{id}"}, method = RequestMethod.DELETE)
    public com.chengyu.core.entity.CommonResult<String> del(@Validated @PathVariable Integer id) throws CommonException, ServiceException {


        receiptUserService.deleteByIdWithMemberId(id,getCurrentMemberId());
//		goodsService.publishGoodsNew(getCurrentShop(), publishForm);
        return com.chengyu.core.entity.CommonResult.success(null);
    }



}



